{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "15a4065b",
      "metadata": {},
      "outputs": [],
      "source": [
        "\"\"\"\n",
        "def  hello(name):\n",
        "    strHello='Hello, '+name\n",
        "    return strHello;\n",
        "\n",
        "print (hello(\"Python!\"))\n",
        "'cd C:\\\\Users\\huige\\AppData\\Local\\Programs\\Python\\Python310\\Scripts'\n",
        "pip install xxx库\n",
        "\"\"\"\n",
        "\n",
        "import requests\n",
        "from lxml import etree\n",
        "import csv\n",
        "\n",
        "file = open('data.csv',mode='a',encoding='utf-8-sig',newline='')\n",
        "csv_write = csv.DictWriter(file,fieldnames=['序号','题目','难度','标签'])\n",
        "csv_write.writeheader()\n",
        "for page in range(0,50):\n",
        "    url = \"https://leetcode.cn/graphql/\"\n",
        "    #header 需要替换\n",
        "    headers = {\n",
        "        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',\n",
        "        'x-csrftoken': 'ggObWAK7ElPsSzvdZxN6yyVaQMvZadnqYhiaGPpdYp4tgyMZDx8bpx77w7P3NHZB',\n",
        "        'Host': 'leetcode.cn',\n",
        "        'Origin': 'https://leetcode.cn',\n",
        "        'random-uuid': '67fcd92f-2e7d-f929-52c7-c603d90b153a',\n",
        "        'Cookies': 'NEW_PROBLEMLIST_PAGE=1; csrftoken=ggObWAK7ElPsSzvdZxN6yyVaQMvZadnqYhiaGPpdYp4tgyMZDx8bpx77w7P3NHZB; gr_user_id=134aa064-7f23-43f6-98e9-2e47bb88063f; a2873925c34ecbd2_gr_session_id=ff4c7ea4-3ecc-4351-864f-2a18ca584c35; a2873925c34ecbd2_gr_session_id_ff4c7ea4-3ecc-4351-864f-2a18ca584c35=true; Hm_lvt_f0faad39bcf8471e3ab3ef70125152c3=1682310448; _gid=GA1.2.722410642.1682310448; _gat_gtag_UA_131851415_1=1; _bl_uid=5mlmwgj2uLXd7hebk81Le4jy5F2p; wechat_state=BlMCaJBH; LEETCODE_SESSION=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuZXh0X2FmdGVyX29hdXRoIjoiLyIsIl9hdXRoX3VzZXJfaWQiOiIyNTc1NTUxIiwiX2F1dGhfdXNlcl9iYWNrZW5kIjoiZGphbmdvLmNvbnRyaWIuYXV0aC5iYWNrZW5kcy5Nb2RlbEJhY2tlbmQiLCJfYXV0aF91c2VyX2hhc2giOiI0YTY1MzExYjNkZjFiNWE0ZjNlM2RhNWVkYzE0N2Y3MDcwYzE2YjNmZTBkOWMyNGM1NWJkYTVkNzZjYTc4MmEwIiwiaWQiOjI1NzU1NTEsImVtYWlsIjoiIiwidXNlcm5hbWUiOiJzaGktamllLXpoaS13YWktZGUtc2hpLWppZSIsInVzZXJfc2x1ZyI6InNoaS1qaWUtemhpLXdhaS1kZS1zaGktamllIiwiYXZhdGFyIjoiaHR0cHM6Ly9hc3NldHMubGVldGNvZGUuY24vYWxpeXVuLWxjLXVwbG9hZC9kZWZhdWx0X2F2YXRhci5wbmciLCJwaG9uZV92ZXJpZmllZCI6ZmFsc2UsIl90aW1lc3RhbXAiOjE2ODIzMTI0NzUuNjQ3NDQ0NywiZXhwaXJlZF90aW1lXyI6MTY4NDg2ODQwMCwidmVyc2lvbl9rZXlfIjowLCJsYXRlc3RfdGltZXN0YW1wXyI6MTY4MjMxMjQ3N30.ffkRNxxk9TCF2iGKtjsmVSmyh4Fwy37aZW2V9azdK6U; _ga=GA1.1.326438699.1682310448; a2873925c34ecbd2_gr_last_sent_sid_with_cs1=ff4c7ea4-3ecc-4351-864f-2a18ca584c35; a2873925c34ecbd2_gr_last_sent_cs1=shi-jie-zhi-wai-de-shi-jie; a2873925c34ecbd2_gr_cs1=shi-jie-zhi-wai-de-shi-jie; _ga_RN3J7V8VS5=GS1.1.1682310591.1.1.1682312485.0; _ga_PDVPZYN3CW=GS1.1.1682312444.1.1.1682312485.0.0.0; Hm_lpvt_f0faad39bcf8471e3ab3ef70125152c3=1682312493'\n",
        "\n",
        "    }\n",
        "    skip_num = page*50\n",
        "    skip = f'{{\"categorySlug\":\"algorithms\",\"skip\":{skip_num},\"limit\":50,\"filters\":{{}}}}'\n",
        "    query = {\n",
        "        'operationName':'problemsetQuestionList',\n",
        "        'query':\"\\n    query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {\\n  problemsetQuestionList(\\n    categorySlug: $categorySlug\\n    limit: $limit\\n    skip: $skip\\n    filters: $filters\\n  ) {\\n    hasMore\\n    total\\n    questions {\\n      acRate\\n      difficulty\\n      freqBar\\n      frontendQuestionId\\n      isFavor\\n      paidOnly\\n      solutionNum\\n      status\\n      title\\n      titleCn\\n      titleSlug\\n      topicTags {\\n        name\\n        nameTranslated\\n        id\\n        slug\\n      }\\n      extra {\\n        hasVideoSolution\\n        topCompanyTags {\\n          imgUrl\\n          slug\\n          numSubscribed\\n        }\\n      }\\n    }\\n  }\\n}\\n   \",\n",
        "        'variables':skip\n",
        "    }\n",
        "\n",
        "    response = requests.post(url,headers=headers,data=query)\n",
        "    response.encoding ='utf-8'\n",
        "    json_data = response.json()\n",
        "    questions = json_data['data']['problemsetQuestionList']['questions']\n",
        "    for data in questions:\n",
        "        frontendQuestionId = data['frontendQuestionId']\n",
        "        difficulty = data['difficulty']\n",
        "        titleCn = data['titleCn']\n",
        "        tags = ''\n",
        "        for tag in data['topicTags']:\n",
        "            tags = tags +'|' +tag['nameTranslated']\n",
        "\n",
        "        # print(titleCn,difficulty,tags,'\\n')\n",
        "\n",
        "        data_dict = {\n",
        "            '序号': frontendQuestionId,\n",
        "            '题目': titleCn,\n",
        "            '难度': difficulty,\n",
        "            '标签':tags\n",
        "        }\n",
        "        csv_write.writerow(data_dict)\n",
        "\n",
        "file.close()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9edd1cf0",
      "metadata": {},
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "import requests\n",
        "from lxml import etree\n",
        "import csv\n",
        "\n",
        "# 从data.csv 获取 算法分类标签\n",
        "file = open('data.csv',mode='r',encoding='utf-8-sig',newline='')\n",
        "csv_reader = csv.DictReader(file)\n",
        "tag_dict = dict()\n",
        "for row in csv_reader:\n",
        "    tags = row['标签']\n",
        "    for tag in tags.split('|'):\n",
        "        if(tag in tag_dict):\n",
        "            val = tag_dict[tag]\n",
        "            tag_dict[tag] = val+1\n",
        "        else:\n",
        "            tag_dict.setdefault(tag,0)\n",
        "\n",
        "print(str(tag_dict))\n",
        "file.close()\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}